package com.befirefly.studentmanager.mapper;

import com.befirefly.studentmanager.entity.StudentDO;
import com.befirefly.studentmanager.req.StudentRequest;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Vector;

/**
 * @Author: 八千里路云和月
 * @Date: 2021/2/8 14:25
 * @Description:
 */
public class StudentMapper {

    private static String path = StudentMapper.class.getClassLoader().getResource("").getPath() + "xml/student.xml";
    private static Document document;

    static {
        try {
            document = new SAXReader().read(path);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static Vector<Vector<Object>> selectBySearchKey(StudentRequest request) {
        Element rootElement = document.getRootElement();
        List<Element> elementList1 = rootElement.elements();
        Vector<Vector<Object>> data = new Vector<>();
        for (Element element : elementList1) {
            Element nameElement = element.element("name");
            String name = nameElement.getTextTrim();
            if (name.contains(request.getSearchKey())) {
                Vector<Object> oneRecord = new Vector<>();
                Long id = Long.valueOf(element.element("id").getTextTrim());
                String no = element.element("no").getTextTrim();
                String home = element.element("home").getTextTrim();
                int cnScore = Integer.parseInt(element.element("cn_score").getTextTrim());
                int enScore = Integer.parseInt(element.element("en_score").getTextTrim());
                int mathScore = Integer.parseInt(element.element("math_score").getTextTrim());
                int totalScore = cnScore + enScore + mathScore;
                oneRecord.addElement(id);
                oneRecord.addElement(name);
                oneRecord.addElement(no);
                oneRecord.addElement(home);
                oneRecord.addElement(cnScore);
                oneRecord.addElement(enScore);
                oneRecord.addElement(mathScore);
                oneRecord.addElement(totalScore);
                data.addElement(oneRecord);
            }
        }
        return data;
    }

    public static Integer selectTotalCount() {
        Element rootElement = document.getRootElement();
        return Integer.valueOf(rootElement.attribute("totalCount").getValue());
    }

    public static Vector<Vector<Object>> selectAll() {
        Element rootElement = document.getRootElement();
        List<Element> elementList1 = rootElement.elements();
        Vector<Vector<Object>> data = new Vector<>();
        for (Element element : elementList1) {
            Vector<Object> oneRecord = new Vector<>();
            Long id = Long.valueOf(element.element("id").getTextTrim());
            String name = element.element("name").getTextTrim();
            String no = element.element("no").getTextTrim();
            String home = element.element("home").getTextTrim();
            int cnScore = Integer.parseInt(element.element("cn_score").getTextTrim());
            int enScore = Integer.parseInt(element.element("en_score").getTextTrim());
            int mathScore = Integer.parseInt(element.element("math_score").getTextTrim());
            int totalScore = cnScore + enScore + mathScore;
            oneRecord.addElement(id);
            oneRecord.addElement(name);
            oneRecord.addElement(no);
            oneRecord.addElement(home);
            oneRecord.addElement(cnScore);
            oneRecord.addElement(enScore);
            oneRecord.addElement(mathScore);
            oneRecord.addElement(totalScore);
            data.addElement(oneRecord);
        }
        return data;
    }

    public static boolean add(StudentDO studentDO) {
        try {
            System.out.println(path);
            Element rootElement = document.getRootElement();
            Integer totalCount = Integer.valueOf(rootElement.attributeValue("totalCount"));
            Integer autoIncrement = Integer.valueOf(rootElement.attributeValue("auto_increment"));
            Element student = rootElement.addElement("student");
            Element id = student.addElement("id");
            id.setText(String.valueOf(autoIncrement));
            Element name = student.addElement("name");
            name.setText(studentDO.getName());
            Element no = student.addElement("no");
            no.setText(studentDO.getNo());
            Element home = student.addElement("home");
            home.setText(studentDO.getHome());
            Element cnScore = student.addElement("cn_score");
            cnScore.setText(String.valueOf(studentDO.getCnScore()));
            Element enScore = student.addElement("en_score");
            enScore.setText(String.valueOf(studentDO.getEnScore()));
            Element mathScore = student.addElement("math_score");
            mathScore.setText(String.valueOf(studentDO.getMathScore()));
            rootElement.attribute("totalCount").setValue(String.valueOf(totalCount + 1));
            rootElement.attribute("auto_increment").setValue(String.valueOf(autoIncrement + 1));
            OutputFormat opf = new OutputFormat("\t", true, "UTF-8");
            opf.setTrimText(true);
            XMLWriter writer = new XMLWriter(new FileOutputStream(path), opf);
            writer.write(document);
            writer.close();
            System.out.println("写入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static StudentDO selectById(long selectedStudentId) {
        Element rootElement = document.getRootElement();
        StudentDO studentDO = new StudentDO();
        List<Element> elementList1 = rootElement.elements();
        for (Element element1 : elementList1) {
            int id = Integer.parseInt(element1.element("id").getTextTrim());
            if (id == selectedStudentId) {
                String name = element1.element("name").getTextTrim();
                String no = element1.element("no").getTextTrim();
                String home = element1.element("home").getTextTrim();
                Integer cnScore = Integer.valueOf(element1.element("cn_score").getTextTrim());
                Integer enScore = Integer.valueOf(element1.element("en_score").getTextTrim());
                Integer mathScore = Integer.valueOf(element1.element("math_score").getTextTrim());
                studentDO.setId((long) id);
                studentDO.setName(name);
                studentDO.setNo(no);
                studentDO.setHome(home);
                studentDO.setCnScore(cnScore);
                studentDO.setEnScore(enScore);
                studentDO.setMathScore(mathScore);
                return studentDO;
            }
        }
        return null;
    }

    public static boolean updateById(StudentDO studentDO) {
        Element rootElement = document.getRootElement();
        List<Element> elementList1 = rootElement.elements();
        for (Element element1 : elementList1) {
            String id = element1.element("id").getTextTrim();
            if (StringUtils.equals(id, String.valueOf(studentDO.getId()))) {
                try {
                    element1.element("name").setText(studentDO.getName());
                    element1.element("no").setText(studentDO.getNo());
                    element1.element("home").setText(studentDO.getHome());
                    element1.element("cn_score").setText(String.valueOf(studentDO.getCnScore()));
                    element1.element("en_score").setText(String.valueOf(studentDO.getEnScore()));
                    element1.element("math_score").setText(String.valueOf(studentDO.getMathScore()));
                    OutputFormat opf = new OutputFormat("\t", true, "UTF-8");
                    opf.setTrimText(true);
                    XMLWriter writer = new XMLWriter(new FileOutputStream(path), opf);
                    writer.write(document);
                    writer.close();
                    System.out.println("写入成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    public static boolean deleteById(Long selectedStudentId) {
        Element rootElement = document.getRootElement();
        int totalCount = Integer.parseInt(rootElement.attribute("totalCount").getValue());
        List<Element> elementList1 = rootElement.elements();
        for (Element element1 : elementList1) {
            String id = element1.element("id").getTextTrim();
            if (StringUtils.equals(id, String.valueOf(selectedStudentId))) {
                try {
                    rootElement.remove(element1);
                    rootElement.attribute("totalCount").setValue(String.valueOf(totalCount - 1));
                    OutputFormat opf = new OutputFormat("\t", true, "UTF-8");
                    opf.setTrimText(true);
                    XMLWriter writer = new XMLWriter(new FileOutputStream(path), opf);
                    writer.write(document);
                    writer.close();
                    System.out.println("删除成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}
